package com.game.logic.items.bonus;

import com.coment.cfg.luban.Tables;
import com.coment.cfg.luban.items.BonusCfg;
import com.coment.game.config.ConfigException;
import com.game.logic.core.comment.Bonus;
import com.xs.tool.formula.DrawCardsFormula;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/// 0.礼包服务
@Slf4j
public class BonusService {
    /// 礼包池 k:礼包id v:礼包
    public static final Map<Integer, Bonus<Integer>> bonusPoll;

    static {
        bonusPoll = new ConcurrentHashMap<>();

        for (BonusCfg cfg : Tables.cfg.get_tbbonuscfg().getDataList()) {
            int k = cfg.id;
            int type = cfg.type;
            ArrayList<Integer> value = cfg.value;

            Bonus<Integer> bonus;
            try {
                if (type == 1) {
                    bonus = new Bonus<>(cfg.group, type, null, value);
                } else if (type == 2) {
                    ArrayList<Integer> weight = cfg.weight;
                    Map<Integer, Integer> valueWeightMap = new HashMap<>();
                    for (int i = 0; i < value.size() && i < weight.size(); i++) {
                        valueWeightMap.put(value.get(i), weight.get(i));
                    }
                    DrawCardsFormula<Integer> map = DrawCardsFormula.fromIntegerMap(valueWeightMap);
                    bonus = new Bonus<>(cfg.group, type, map, null);
                } else {
                    throw new ConfigException("礼包配置错误");
                }
            } catch (ConfigException e) {
                continue;
            }
            bonusPoll.put(k, bonus);
        }
        log.debug("\ud83d\udcc4 加载到 {} 个礼包配置", BonusService.bonusPoll.size());
    }
}
